package com.thomasmoerman.dci3.registry

import org.springframework.context.annotation._
import org.springframework.beans.factory.annotation._
import scala.reflect._
import java.util.{Properties => Props}
import com.thomasmoerman.dci3.frw._
import com.thomasmoerman.dci3.tmc._
import com.thomasmoerman.dci3.pbc._
import javax.sql.DataSource
import org.hibernate._
import org.springframework.transaction.PlatformTransactionManager
import org.springframework.orm.hibernate3.HibernateTransactionManager
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean

@Configuration
class Registry {
  
  @Bean def payBillsContext = new PayBillsContextFactoryBean
  
  @Bean def dataAccessHelper = new DataAccessHelper
  
  @Bean def dataSource = 
	new C3P0DataSourceFactory(
	  dataSourceName = "PBC2", 
	  user = "root", 
	  password = "admin",
	  jdbcUrl = "jdbc:mysql://localhost:3306/PBC2").instance
  
  @Bean def sessionFactoryBean = {
	val sfb = new AnnotationSessionFactoryBean
	
	sfb.setDataSource(dataSource)
	
	sfb.setHibernateProperties{
	  val props = new Props
	  
	  props.put("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect")
	  props.put("hibernate.show_sql", "false")
	  props.put("hibernate.format_sql", "true")
	  props.put("hibernate.hbm2ddl.auto", "create")
	  
	  props
	}
	
	sfb.setPackagesToScan(Array("com.thomasmoerman.dci3.domain"))
	
	sfb
  }
  
  @Bean def transactionManager: PlatformTransactionManager = {
	val txm = new HibernateTransactionManager 
	
	txm.setSessionFactory(sessionFactoryBean.getObject)
	
	txm
  }
  
}